<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Xhsmhq;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\DeptTable;
use App\Models\Dept;

class XhsmhqController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Xhsmhq(), function (Grid $grid) {
            $grid->quickSearch('qcname', 'dept.deptname')->placeholder('输入部门或器材名搜索...');
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用刷新按钮
            $grid->disableRefreshButton();
            // 禁用行选择器
            $grid->disableRowSelector();
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id', 'desc');

            $grid->model()->with(['dept']);
            // $grid->column('id')->sortable();
            $grid->column('qcbianhao');
            $grid->column('qcname');
            $grid->column('dept.deptname', '部门名称');
            $grid->column('weizhi');


            $grid->column('qczrr');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Xhsmhq(), function (Show $show) {
            $show->field('id');
            $show->field('dept_id');
            $show->field('weizhi');
            $show->field('qcname');
            $show->field('qcbianhao');
            $show->field('qczrr');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Xhsmhq(), function (Form $form) {
            $form->display('id');
            $form->selectTable('dept_id', '所属部门：')
                ->title('部门列表')
                ->from(DeptTable::make())
                ->model(Dept::class, 'id', 'deptname')->required();
            $form->text('weizhi')->placeholder('例如：2号楼11层')->required();
            $form->text('qcname')->placeholder('例如：消火栓、灭火器、常闭防火门')->required();
            $form->text('qcbianhao')->placeholder('例如：XHS/MHQ-楼号-楼层-顺序号')->required();
            $form->text('qczrr')->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
